{% extends "personal_center/base.html" %}

{% block page_link %}
    <link rel="stylesheet" href="../../static/system_setup/css/style.css">
    <link rel="stylesheet" href="../../static/public_file/css/DepartmentSelection.css">
    <link rel="stylesheet" href="../../static/public_file/css/PersonnelChooser.css">
{% endblock %}


{% block style %}
    <style>
        .add_class {
            background: #1E90FF;
        }
        .file {
            position: relative;display: inline-block;background: #D0EEFF;border: 1px solid #99D3F5;border-radius: 4px;padding: 4px 12px;overflow: hidden;color: #1E88C7;text-decoration: none;text-indent: 0;line-height: 20px;
        }
        .file input {
            position: absolute;font-size: 100px;right: 0;top: 0;opacity: 0;
        }
        .file:hover {
            background: #AADFFD;border-color: #78C3F3;color: #004974;text-decoration: none;
        }
        .div_clase {
            display: inline-block;width: 300px;height: 30px;line-height: 30px;font-size: 14px;color: #0b0b0b;
        }
    </style>
{% endblock %}


{% block right_content %}
    <div id="new_issues" style="width: 100%;height: 100%;overflow: auto;background-color: white;">
        <div style="width: 100%;height: 40px;border-bottom: #cccccc solid 1px;text-align: right;padding-right: 1%;">
            <button id="save_but" onclick="save_data()" style="width: 100px;height: 30px;margin-top: 5px;background: #2db7f5;color: white;border: #2db7f5 solid 1px;border-radius: 3px;">
                保&nbsp;&nbsp;存
            </button>
            <button id="summit_but" onclick="submit_data()" style="width: 100px;height: 30px;margin-top: 5px;background-color: #1E90FF;color: white;border: #1E90FF solid 1px;border-radius: 3px;margin-left: 10px;">
                提&nbsp;&nbsp;交
            </button>
        </div>

        <div id="party_committee" style="width: 100%;height: 93%;">
            <div style="width: 100%;height: 100%;overflow: auto;margin-top: 1%;">
                <div style="width: 100%;">
                    <table style="width: 90%;margin-left: 5%;">
                        <tr style="width: 100%;">
                            <td style="width: 100%;height: 35px;line-height: 20px;text-align: center;border: #cfcfcf solid 1px;"
                                colspan="4"><h4>京山市人民医院<br>基建维修改造项目申请（审批）表</h4></td>
                        </tr>
                        <tr style="width: 100%;">
                            <td style="width: 100%;height: 35px;line-height: 35px;padding-left: 1%;background: #F5F5F5;border: #cfcfcf solid 1px;color: #1E90FF;"
                                colspan="4">需求科室基本信息
                            </td>
                        </tr>
                        <tr style="width: 100%;">
                            <td style="width: 15%;height: 35px;line-height: 35px;border: #cccccc solid 1px;text-align: right;padding-right: 0.5%;">
                                <p style="width: 80px;float: right;text-align-last: justify;">申请人</p>
                            </td>
                            <td id="apply_user" style="width: 35%;height: 35px;line-height: 35px;border: #cccccc solid 1px;border-left: none;padding-left: 0.5%;">{{ realname }}</td>
                            <td style="width: 15%;height: 35px;line-height: 35px;border: #cccccc solid 1px;text-align: right;padding-right: 0.5%;">
                                <p style="width: 80px;float: right;text-align-last: justify;">联系电话</p>
                            </td>
                            <td style="width: 35%;height: 35px;line-height: 35px;border: #cccccc solid 1px;border-left: none;padding-left: 0.5%;">{{ my_obj.mobile }}</td>
                        </tr>
                        <tr style="width: 100%;">
                            <td style="width: 15%;height: 35px;line-height: 35px;border: #cccccc solid 1px;text-align: right;padding-right: 0.5%;">
                                <p style="width: 80px;float: right;text-align-last: justify;">申请部门</p>
                            </td>
                            <td style="width: 35%;height: 35px;line-height: 35px;border: #cccccc solid 1px;border-left: none;padding-left: 0.5%;">{{ my_obj.company.name }}</td>
                            <td style="width: 15%;height: 35px;line-height: 35px;border: #cccccc solid 1px;text-align: right;padding-right: 0.5%;">
                                <p style="width: 80px;float: right;text-align-last: justify;">申请时间</p>
                            </td>
                            <td style="width: 35%;height: 35px;line-height: 35px;border: #cccccc solid 1px;border-left: none;padding-left: 0.5%;">{{ apply_time }}</td>
                        </tr>
                        <tr style="width: 100%;">
                            <td style="width: 15%;height: 35px;line-height: 35px;border: #cccccc solid 1px;text-align: right;padding-right: 0.5%;border-top: none;">
                                <p style="width: 80px;float: right;text-align-last: justify;">申请内容</p>
                            </td>
                            <td style="width: 35%;height: 35px;line-height: 35px;border: #cccccc solid 1px;border-left: none;padding-left: 0.5%;border-top: none;outline: none;" id="reason" contenteditable="true"></td>
                            <td style="width: 15%;height: 35px;line-height: 35px;border: #cccccc solid 1px;text-align: right;padding-right: 0.5%;border-top: none;">
                                <p style="width: 80px;float: right;text-align-last: justify;">是否应急</p>
                            </td>
                            <td style="width: 35%;height: 35px;line-height: 35px;border: #cccccc solid 1px;border-left: none;padding-left: 0.5%;border-top: none;outline: none;">
                                <input type="radio" name="emergency" value="1"> 是
                                <input type="radio" name="emergency" value="0" style="margin-left: 20px;" checked> 否
                            </td>
                        </tr>
                        <tr style="width: 100%;">
                            <td style="width: 15%;height: 35px;line-height: 35px;border: #cccccc solid 1px;text-align: right;padding-right: 0.5%;border-top: none;letter-spacing: 1.2px;">
                                <p style="width: 80px;float: right;text-align-last: justify;">备注</p>
                            </td>
                            <td id="note" colspan="3" style="width: 85%;height: 35px;line-height: 35px;border: #cccccc solid 1px;border-left: none;padding-left: 0.5%;border-top: none;outline: none;" contenteditable="true"></td>
                        </tr>
                    </table>
                </div>
                <div style="width: 100%;">
                    <div style="width: 90%;height: 40px;line-height: 40px;margin-left: 5%;background-color: #F5F5F5;color: #1E90FF;padding-left: 1%;border: #cccccc solid 1px;border-top: none;border-bottom: none;padding-right: 1%;">
                        上传附件
                        <a href="javascript:;" class="file" style="vertical-align: top;margin-top: 5px;float: right;">
                            上传文件
                            <input type="file" name="party_main_body" id="party_main_body"
                                   onchange="upload_accessory('file1', 'party_main', 'party_main_body')">
                        </a>
                    </div>
                    <table style="width: 90%;margin-left: 5%;">
                        <thead>
                        <tr>
                            <td style="width: 5%;border: #cccccc solid 1px;text-align: center;height: 35px;line-height: 35px;">
                                序号
                            </td>
                            <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;height: 35px;line-height: 35px;">
                                文件名称
                            </td>
                            <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;height: 35px;line-height: 35px;">
                                文件大小
                            </td>
                            <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;height: 35px;line-height: 35px;">
                                上传时间
                            </td>
                            <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;height: 35px;line-height: 35px;">
                                上传人员
                            </td>
                            <td style="width: 15%;border: #cccccc solid 1px;border-left: none;text-align: center;height: 35px;line-height: 35px;">
                                操作
                            </td>
                        </tr>
                        </thead>
                        <tbody style="overflow: auto;" id="party_main"></tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <div id="del_adjunct"
         style="z-index: 1000;position: fixed;background-color: rgba(36, 36, 36, 0.5);width: 100%;height: 100%;top: 0;left: 0;right: 0;bottom: 0;display: none;">
        <div style="top: 50%;left: 50%;width: 400px;height: 150px;background: white;transform:translate(-50%,-50%);border: #dfdfdf solid 1px;position: absolute;border-radius: 4px;">
            <div style="width: 100%;height: 100px;text-align: center;line-height: 100px;">请查看附件，删除无用的附件！</div>
            <hr>
            <div style="width: 100%;height: 50px;text-align: right;padding-right: 5%;">
                <button onclick="del_adjunct_button()"
                        style="width: 50px;height: 30px;line-height: 30px;margin-top: 10px;background: #00a0e9;color: white;border-radius: 4px;border: #00a0e9 solid 1px;">
                    确认
                </button>
            </div>
        </div>
    </div>
{% endblock %}

{% block script %}
    <script>
        window.onload = function () {
            document.getElementById("loading").style.display = "none";
            document.getElementById("personage").className = "add_class";
            document.getElementById("sponsor").className = "add-nav-active";
        };

        // 上传正文
        function upload_accessory(file_type, tbody_id, accessory_type) {
            document.getElementById("loading").style.display = "inline-block";
            var form = new FormData();
            var files = document.getElementById(accessory_type).files[0];
            form.append(file_type, files);
            form.append('number', "{{ uuid | safe }}")
            form.append("source", "JJWXGZ")
            var xhr = new XMLHttpRequest();
            xhr.open('post', "/administrative/topics_attachments_upload", true);
            xhr.send(form);
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        document.getElementById("loading").style.display = "none";
                        var JsonDate = JSON.parse(xhr.responseText);
                        console.log(JsonDate);
                        if (JsonDate["code"] === 400) {
                            alert("请上传xlsx格式文件！")
                        }
                        creat_table(JsonDate, tbody_id);
                        document.getElementById("party_main_body").value = "";
                    }
                }
            };
        }

        // 创建附件列表
        function creat_table(JsonData, tbody_id) {
            var t_table = document.getElementById(tbody_id);
            if (Array.isArray(JsonData["pdf_path"])) {
                for (var i = 0; i < JsonData["pdf_path"].length; i++) {
                    var tr = document.createElement("tr");var td1 = document.createElement("td");var td2 = document.createElement("td");var td3 = document.createElement("td");var td4 = document.createElement("td");var td5 = document.createElement("td");var td6 = document.createElement("td");
                    td1.innerHTML = t_table.children.length + 1;td1.style.textAlign = "center";td1.style.border = "#cccccc solid 1px";td1.style.borderTop = "none";
                    td2.innerHTML = JsonData["file_name"];td2.style.paddingLeft = "1%";td2.style.border = "#cccccc solid 1px";td2.style.borderLeft = "none";td2.style.borderTop = "none";
                    td3.innerHTML = JsonData["file_size"];td3.style.textAlign = "center";td3.style.border = "#cccccc solid 1px";td3.style.borderLeft = "none";td3.style.borderTop = "none";
                    td4.innerHTML = JsonData["upload_time"];td4.style.textAlign = "center";td4.style.border = "#cccccc solid 1px";td4.style.borderLeft = "none";td4.style.borderTop = "none";
                    td5.innerHTML = JsonData["uploader"];td5.style.textAlign = "center";td5.style.border = "#cccccc solid 1px";td5.style.borderLeft = "none";td5.style.borderTop = "none";
                    td6.innerHTML = "<a target='_blank' href='" + JsonData["pdf_path"][i]["pdf_path"] + "'>查看</a> | <a style='color: red;' href='#' onclick='RemoveTr(this, \"" + tbody_id + "\", \"" + JsonData["pdf_path"][i]["id"] + "\")'>删除</a>";
                    td6.style.textAlign = "center";td6.style.border = "#cccccc solid 1px";td6.style.borderLeft = "none";td6.style.borderTop = "none";
                    tr.appendChild(td1);tr.appendChild(td2);tr.appendChild(td3);tr.appendChild(td4);tr.appendChild(td5);tr.appendChild(td6);
                    t_table.appendChild(tr);
                }
                if (JsonData["pdf_path"].length > 1) {
                    document.getElementById("del_adjunct").style.display = 'block';
                }
            } else {
                tr = document.createElement("tr");td1 = document.createElement("td");td2 = document.createElement("td");td3 = document.createElement("td");td4 = document.createElement("td");td5 = document.createElement("td");td6 = document.createElement("td");
                td1.innerHTML = t_table.children.length + 1;td1.style.textAlign = "center";td1.style.border = "#cccccc solid 1px";td1.style.borderTop = "none";
                td2.innerHTML = JsonData["file_name"];td2.style.paddingLeft = "1%";td2.style.border = "#cccccc solid 1px";td2.style.borderLeft = "none";td2.style.borderTop = "none";
                td3.innerHTML = JsonData["file_size"];td3.style.textAlign = "center";td3.style.border = "#cccccc solid 1px";td3.style.borderLeft = "none";td3.style.borderTop = "none";
                td4.innerHTML = JsonData["upload_time"];td4.style.textAlign = "center";td4.style.border = "#cccccc solid 1px";td4.style.borderLeft = "none";td4.style.borderTop = "none";
                td5.innerHTML = JsonData["uploader"];td5.style.textAlign = "center";td5.style.border = "#cccccc solid 1px";td5.style.borderLeft = "none";td5.style.borderTop = "none";
                td6.innerHTML = "<a target='_blank' href='" + JsonData["pdf_path"] + "'>查看</a> | <a style='color: red;' href='#' onclick='RemoveTr(this, \"" + tbody_id + "\", \"" + JsonData["id"] + "\")'>删除</a>";
                td6.style.textAlign = "center";td6.style.border = "#cccccc solid 1px";td6.style.borderLeft = "none";td6.style.borderTop = "none";
                tr.appendChild(td1);tr.appendChild(td2);tr.appendChild(td3);tr.appendChild(td4);tr.appendChild(td5);tr.appendChild(td6);
                t_table.appendChild(tr);
            }
        }

        function RemoveTr(_this, tbody_id, wj_id) {
            var table = document.getElementById(tbody_id);
            var trNode = $(_this).parent().parent();
            trNode.remove();

            for (var i = 0; i < table.children.length; i++) {
                table.children[i].children[0].innerHTML = i + 1;
            }

            var xhr = new XMLHttpRequest();
            var local_host = window.location.host;          // 获取当前IP
            xhr.open('post', "/administrative/delete_attachments_view", true);
            xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
            xhr.send("id=" + String(wj_id));
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        document.getElementById("loading").style.display = "none";
                        var JsonDate = JSON.parse(xhr.responseText);
                        console.log(JsonDate);
                        alert("删除成功！")
                    }
                }
            };
        }

        function del_adjunct_button() {
            document.getElementById("del_adjunct").style.display = 'none';
        }

        function save_data() {
            var form = new FormData();
            var reason = document.getElementById("reason").innerText;             // 申请理由
            var note = document.getElementById("note").innerText;             // 备注
            var emergency = $("input[type=radio][name=emergency]:checked").val();

            form.append('reason', reason);
            form.append('emergency', emergency);
            form.append('note', note);
            form.append('fj_number', "{{ uuid | safe }}");
            document.getElementById("loading").style.display = "inline-block";
            var xhr = new XMLHttpRequest();
            xhr.open('post', "/infrastructure-process/InfrastructureRenovationDraft.html", true);
            xhr.send(form);
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        var JsonDate = JSON.parse(xhr.responseText);
                        if (JsonDate["code"] === 200) {
                            alert("保存成功！");
                            window.location = "/infrastructure-process/InfrastructureRenovationList.html";
                        } else {
                            alert(JsonDate['message']);
                            document.getElementById("loading").style.display = "none";
                        }
                    }
                }
            };
        }

        function submit_data() {
            var form = new FormData();
            var reason = document.getElementById("reason").innerText;             // 申请理由
            var note = document.getElementById("note").innerText;             // 备注
            var emergency = $("input[type=radio][name=emergency]:checked").val();

            if (!reason) {
                alert("请输入申请内容！");
                return false
            }

            form.append('reason', reason);
            form.append('emergency', emergency);
            form.append('note', note);
            form.append('fj_number', "{{ uuid | safe }}");
            document.getElementById("loading").style.display = "inline-block";
            var xhr = new XMLHttpRequest();
            var local_host = window.location.host;          // 获取当前IP
            xhr.open('post', "/infrastructure-process/InfrastructureRenovationIndex.html", true);
            xhr.send(form);
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        var JsonDate = JSON.parse(xhr.responseText);
                        if (JsonDate["code"] === 200) {
                            alert("创建成功");
                            window.location = "/infrastructure-process/InfrastructureRenovationList.html";
                        } else {
                            alert(JsonDate['message']);
                            document.getElementById("loading").style.display = "none";
                        }
                    }
                }
            };
        }
    </script>
{% endblock %}
